package MathTextModeller
{
	import MathText.WordProblem;
	import MathText.WordProblemDisplay;
	
	import MyDisplayObjects.DraggableImage;
	import MyDisplayObjects.DraggableShape;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/** This is the highest level object for the model display portion of the tool.  
	 * This object allows a user to manipulate objects to model the given word problem */
	public class ModelDisplayContainer extends MovieClip
	{
		// private vars, inaccessible outside of class
		private var wordProblemDisplay:WordProblemDisplay;
		private var problemIntro:ProblemIntro;
		private var sentenceModeller:SentenceModeller;
		private var prevInitialToolDisplay:String="";
		private var sentenceDisplays:Array;
		private var actionModeller:ActionModeller;
		private var sentenceCount:uint = 0; // number of sentences we have modelled (initially);
		private var optionsXML:XML, imagesXML:XML, errorsXML:XML;
		
		public function ModelDisplayContainer(width:Number, height:Number, wordProblemDisplay:WordProblemDisplay, optionsXML:XML, imagesXML:XML, errorsXML:XML)
		{
			this.optionsXML = optionsXML;
			this.imagesXML = imagesXML;
			this.errorsXML = errorsXML;
			this.graphics.beginFill(0xEEEEFF, 1);
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();			
			this.wordProblemDisplay = wordProblemDisplay;
			showIntro();
			//example();
		}
		
		/** Show the problem display intro, when completed move on */
		private function showIntro():void
		{
			problemIntro = new ProblemIntro (this.width, this.height, "Please read the problem carefully before beginning.");
			problemIntro.addEventListener( Event.COMPLETE, handleProblemIntroComplete);
			this.addChild(problemIntro);
		}
		
		/** Remove problem intro, move onto sentence modelling */
		private function handleProblemIntroComplete (event:Event):void
		{
			problemIntro.removeEventListener( Event.COMPLETE, handleProblemIntroComplete);
			this.removeChild(problemIntro);
			
			// model sentences
			sentenceDisplays = new Array();
			modelNextSentence();
		}
		/** This function initiates a new SentenceModeller, adds an event listener to it */
		private function modelNextSentence():void
		{
			if (sentenceCount < wordProblemDisplay.wordProblem.numSentencesToModel)
			{
				if (optionsXML.child("sentence").(attribute("hidePeripheralText") == "true").length() > 0)
				{
					wordProblemDisplay.displaySubstring(wordProblemDisplay.wordProblem.sentences[sentenceCount]);
				} else
				{
					wordProblemDisplay.highlightSubstring(wordProblemDisplay.wordProblem.sentences[sentenceCount]);
				}
				sentenceModeller = new SentenceModeller(this.width, this.height, wordProblemDisplay.wordProblem, sentenceCount, optionsXML, imagesXML, errorsXML, prevInitialToolDisplay);
				sentenceModeller.addEventListener( Event.COMPLETE, handleNextSentenceModellerComplete);
				this.addChild(sentenceModeller);
			} else
			{
				displayActionModeller();
			}
		}
			private function handleNextSentenceModellerComplete (event:Event):void
			{
				wordProblemDisplay.highlightSubstring("");
				this.prevInitialToolDisplay = sentenceModeller.currentFaceType;
				sentenceModeller.removeEventListener( Event.COMPLETE, handleNextSentenceModellerComplete);
				sentenceDisplays.push(sentenceModeller.sentenceDisplay);
				this.removeChild(sentenceModeller);
				sentenceCount++;
				modelNextSentence();
			}
		
		/** Displays a sentence panel that shows all three (or whatever) sentences adjacent to each other so that objects can be moved around */ 
		public function displayActionModeller():void
		{
			actionModeller = new ActionModeller(sentenceDisplays, wordProblemDisplay.wordProblem, errorsXML);
			actionModeller.addEventListener(Event.COMPLETE, handleActionModellerComplete);
			actionModeller.addEventListener(Event.CANCEL, handleActionModellerCancel);
			this.addChild(actionModeller);
		}
		
		/** This function is called when the user decides to edit a sentence */
		private function handleActionModellerCancel (event:Event):void
		{
			actionModeller.removeEventListener(Event.COMPLETE, handleActionModellerComplete);
			actionModeller.removeEventListener(Event.CANCEL, handleActionModellerCancel);
			// go back into sentence modelling
			for (var i:uint=0; i < sentenceDisplays.length; i++)
			{
				if (sentenceDisplays[i] == actionModeller.currentSentenceDisplay) sentenceCount = i;		
			}
			this.removeChild(actionModeller);
			modelSentence();
		}
		/** This function initiates a new SentenceModeller, adds an event listener to it */
		private function modelSentence():void
		{
			if (sentenceCount < wordProblemDisplay.wordProblem.numSentencesToModel)
			{	
				wordProblemDisplay.highlightSubstring(wordProblemDisplay.wordProblem.sentences[sentenceCount]);
				sentenceModeller = new SentenceModeller(this.width, this.height, wordProblemDisplay.wordProblem, sentenceCount, optionsXML, imagesXML, errorsXML, this.prevInitialToolDisplay, sentenceDisplays[sentenceCount]);
				sentenceModeller.addEventListener( Event.COMPLETE, handleSentenceModellerComplete);
				this.addChild(sentenceModeller);
			} else
			{
				displayActionModeller();
			}
		}
		private function handleSentenceModellerComplete (event:Event):void
		{
			wordProblemDisplay.highlightSubstring("")
			sentenceModeller.removeEventListener( Event.COMPLETE, handleSentenceModellerComplete);
			this.removeChild(sentenceModeller);
			displayActionModeller();
		}
		/** This function is called when the user has completed the modelling of the actions in this panel */
		private function handleActionModellerComplete(event:Event):void
		{
			actionModeller.removeEventListener(Event.COMPLETE, handleActionModellerComplete);
			actionModeller.removeEventListener(Event.CANCEL, handleActionModellerCancel);
			this.dispatchEvent( new Event(Event.COMPLETE));
		}
			
	}
}